package com.manxi.mapper;

import com.manxi.entity.AddressBook;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AddressBookMapper {


    /**
     * 查询当前登录用户的所有地址信息
     *
     * @param addressBook
     * @return
     */
    @Select("<script> " +
            "select * from address_book " +
            "where 1 = 1 " +
            "<if test='userId != null'>AND user_id = #{userId}</if> " +
            "<if test='phone != null'>AND phone = #{phone}</if> " +
            "<if test='isDefault != null'>AND is_default = #{isDefault}</if> " +
            "</script>")
    List<AddressBook> list(AddressBook addressBook);

    /**
     * 根据id查询地址
     *
     * @param id
     * @return
     */
    @Select("select * from address_book where id = #{id}")
    AddressBook getById(Long id);

    /**
     * 新增用户地址
     *
     * @param addressBook
     */
    @Insert("insert into address_book (user_id, consignee, phone, sex, province_code, " +
            "province_name, city_code, city_name, district_code, district_name," +
            " detail, label, is_default)" +
            "values(#{userId}, #{consignee}, #{phone}, #{sex}, #{provinceCode}, #{provinceName}," +
            " #{cityCode}, #{cityName},#{districtCode}, #{districtName}, #{detail}, #{label}, #{isDefault})")
    void insert(AddressBook addressBook);

    /**
     * 根据id修改地址
     *
     * @param addressBook
     */
    @Update("<script> " +
            "update address_book set " +
            "<if test='consignee != null'>consignee = #{consignee},</if> " +
            "<if test='sex != null'>sex = #{sex},</if> " +
            "<if test='phone != null'>phone = #{phone},</if> " +
            "<if test='provinceCode != null'>province_code = #{provinceCode},</if> " +
            "<if test='cityCode != null'>city_code = #{cityCode},</if> " +
            "<if test='districtCode != null'>district_code = #{districtCode},</if> " +
            "<if test='provinceName != null'>province_name = #{provinceName},</if> " +
            "<if test='cityName != null'>city_name = #{cityName},</if> " +
            "<if test='districtName != null'>district_name = #{districtName},</if> " +
            "<if test='detail != null'>detail = #{detail},</if> " +
            "<if test='label != null'>label = #{label},</if> " +
            "<if test='isDefault != null'>is_default = #{isDefault}</if> " +
            "where id = #{id}" +
            "</script>")
    void update(AddressBook addressBook);

    /**
     * 根据id删除地址
     *
     * @param id
     */
    @Delete("delete from address_book where id = #{id}")
    void deleteById(Long id);

    /**
     * 根据 用户id修改 是否默认地址
     *
     * @param addressBook
     */
    @Update("update address_book set is_default = #{isDefault} where user_id = #{userId}")
    void updateIsDefaultByUserId(AddressBook addressBook);

}
